<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/5
 * Time: 下午4:49
 */
class BroadcastController extends AdminBaseController {
	
	public function index(){
		$this->setTitle('消息广播列表', '游戏内的消息广播');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addSeaSpecialFilter(true);
		$filter->addSeaOsFilterAll();
		$filter->addOssServerAll();
		//$filter->addSeaOsFilter();
		//$filter->addOssServer(true);
        $filter->addFilter('begin', 'input', '创建时间', array('date' => 'day'))
            ->addFilter('end', 'input', '至', array('date' => 'day'));
	
		$params = $filter->get();
		$header = BroadcastService::indexHeader();
		$data   = BroadcastService::indexData($params);
	
		$grid->addGrid($header, $data);
	
		$this->out['filter'] = $filter->show();
		$this->out['grid'] = $grid->show();
	}

    

    public function add(){
        $this->setTitle('新增消息广播');

        $this->out['serverList'] = ServerService::getOptions();

        $this->out['typeList'] = BroadcastService::typeList();
    }
    
    public function doAdd(){
    	$result = BroadcastService::doAdd($_POST);
    	
    	if($result['state']){
    		$this->showMessage('添加成功', PubFunc::buildUrl('/oss/?r=broadcast/index'));
    	}else{
    		$this->showMessage($result['msg']);
    	}
    }

    
    public function delete(){
    	$id = $this->R('id');
    
    	$result = BroadcastService::delete($id);
    
    	if($result !== false){
    		$this->showMessage('删除成功', PubFunc::buildUrl("/oss/?r=broadcast/index"));
    	}else{
    		$this->showMessage('删除失败');
    	}
    }
    
    public function synchro(){
    	$ids = PubRequest::R('ids');
    	if(PubFunc::isAjax()){
    		$servers = PubRequest::R('servers');    		
    		$result = BroadcastService::synchro($ids,$servers);    		
    		$this->out = array(
    				'status' => 1,
    				'msg' => $result,
    				'data'    => ''
    		);
    		$this->outType = 'json';
    	}else{
    		$list = PubServer::getChannelServer();
    
    		$this->out['list'] = $list;
    		$this->out['ids'] = $ids;
    		$this->layout = '';
    
    		$url = PubFunc::buildUrl("/oss/?r=broadcast/synchro");
    		$this->out['url'] = $url;

    
    	}
    }
    
    
    public function synchro1(){
    	$ids = PubRequest::R('ids');
    	if(PubFunc::isAjax()){
//     		$servers = PubRequest::R('servers');
//     		$result = BroadcastService::synchro($ids,$servers);
    		$this->out = array(
    				'status' => 1,
    				'msg' => $result,
    				'data'    => ''
    		);
    		$this->outType = 'json';
    	}else{
    		$specialList = SpecialServerService::specialList();
    		$this->out['specialList'] = $specialList;
    		$this->out['ids'] = $ids;
    		$this->layout = '';
    
    		$url = PubFunc::buildUrl("/oss/?r=broadcast/synchro");
    		$this->out['url'] = $url;
    		
    		$speUrl = PubFunc::buildUrl("/oss/?r=specialServer/getServerChannel");
    		$this->out['speUrl'] = $speUrl;
    
    
    	}
    }

    

    
}